package org.unit.testmgmt.generator.wizard;

import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.unit.testmgmt.messages.Messages;
import org.unit.testmgmt.tml.UTMModel;
import org.unit.testmgmt.ui.generator.wizard.GeneratorWizardMainPage;
import org.unit.testmgmt.ui.generator.wizard.GeneratorWizardTestBasesPage;
import org.unit.testmgmt.ui.generator.wizard.GeneratorWizardTestCasesPage;

/**
 * Main-class for the generator wizard.
 * 
 * @author Robert Streng
 * 
 */
public class GeneratorWizard extends Wizard implements INewWizard {

	private GeneratorWizardMainPage mainPage;
	private GeneratorWizardTestCasesPage testcasesPage;
	private GeneratorWizardTestBasesPage testbasesPage;
	private boolean finished = false;
	private UTMModel utmModel;
	private boolean initialized = false;

	public GeneratorWizard(UTMModel model) {
		this.utmModel = model;
	}

	/**
	 * Adding the page to the wizard.
	 */

	public void addPages() {
		String title = Messages.GeneratorWizard_Unit_test_class_generator;

		mainPage = createMainPage(title);
		addPage(mainPage);

		testbasesPage = createTestbasesPage(title, mainPage);
		addPage(testbasesPage);

		testcasesPage = createTestcasesPage(title, mainPage, testbasesPage);
		addPage(testcasesPage);
	}

	@Override
	public void createPageControls(Composite pageContainer) {
		super.createPageControls(pageContainer);
	}

	/**
	 * Initializes the pages.
	 */
	public void initPages() {

		if (initialized) {
			return;
		}
		else {
			initialized = true;
		}

		getMainPage().init();
		getTestbasesPage().init();
		getTestcasesPage().init();
	}

	/**
	 * Creates the test bases page
	 * @param title
	 * @param mainPage
	 * @return test bases page
	 */
	private GeneratorWizardTestBasesPage createTestbasesPage(String title,
			GeneratorWizardMainPage mainPage) {
		title += Messages.GeneratorWizard_testbases;
		String description = Messages.GeneratorWizard_description_testbases;
		GeneratorWizardTestBasesPage page = new GeneratorWizardTestBasesPage(title, description,
				Messages.GeneratorWizard_testbasesPage, utmModel, mainPage);
		return page;
	}

	/**
	 * Creates the test cases page.
	 * 
	 * @param title
	 * @param mainPage
	 * @param testbasesPage
	 * @return test cases page
	 */
	private GeneratorWizardTestCasesPage createTestcasesPage(String title,
			GeneratorWizardMainPage mainPage, GeneratorWizardTestBasesPage testbasesPage) {
		title += Messages.GeneratorWizard_testcases;
		String description = Messages.GeneratorWizard_description_testcases;
		GeneratorWizardTestCasesPage page = new GeneratorWizardTestCasesPage(title, description,
				Messages.GeneratorWizard_testcasesPage, utmModel, mainPage, testbasesPage);
		return page;
	}

	/**
	 * Creates the main page.
	 * 
	 * @param title
	 * @return the main page
	 */
	private GeneratorWizardMainPage createMainPage(String title) {
		String description = Messages.GeneratorWizard_description_main;
		GeneratorWizardMainPage page = new GeneratorWizardMainPage(title, description,
				Messages.GeneratorWizard_mainPage, utmModel) {

			@Override
			public void setPageComplete(boolean complete) {
				super.setPageComplete(complete);
				if (complete && !isPagesInitialized())
					testcasesPage.init();
			}

		};

		return page;
	}

	/**
	 * Returns if the pages are initialized.
	 * @return true if pages are initialized
	 */
	protected boolean isPagesInitialized() {
		return this.initialized;
	}

	/**
	 * This method is called when 'Finish' button is pressed in the wizard.
	 */
	public boolean performFinish() {
		getMainPage().update();
		getTestbasesPage().update();
		getTestcasesPage().update();

		finished = true;
		return true;
	}

	/**
	 * Returns if the processing is finished.
	 * @return true if the processing is finished.
	 */
	public boolean isFinished() {
		return finished;
	}

	@Override
	public void init(IWorkbench workbench, IStructuredSelection selection) {

	}

	/**
	 * @return main page
	 */
	private GeneratorWizardMainPage getMainPage() {
		return mainPage;
	}

	/**
	 * @return test cases page
	 */
	private GeneratorWizardTestCasesPage getTestcasesPage() {
		return testcasesPage;
	}

	/**
	 * @return test bases page
	 */
	private GeneratorWizardTestBasesPage getTestbasesPage() {
		return testbasesPage;
	}

	@Override
	public boolean needsProgressMonitor() {
		return true;
	}

}